package com.demo.java.OD1_50.OD50;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【最短耗时】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145585144
 */
public class OdMain {
    public static void main(String[] args) {
        try {
            Scanner s = new Scanner(System.in);
            String[] splitStr = s.nextLine().split(",");
            int[] jobs = Arrays.stream(splitStr).mapToInt(Integer::parseInt).toArray();
            int n = s.nextInt();  // 读取并行的最大任务数量
            solveMethod(jobs, n);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("input error");
        }
    }

    private static void solveMethod(int[] jobs, int n) {
        // 使用哈希表记录每个任务的出现次数
        Map<Integer, Long> myJobHashMap = new HashMap<>(jobs.length);
        for (int job : jobs) {
            myJobHashMap.put(job, myJobHashMap.getOrDefault(job, 0L) + 1);
        }

        // 将任务按数量降序排列
        List<Long> myList = myJobHashMap.values().stream().sorted((i1, i2) -> Long.compare(i2, i1))
                .collect(Collectors.toList());

        int allTime = 0;  // 总时间
        while (myList.size() > 0) {
            if (myList.get(0) == 1 && myList.size() == 1) {
                ++allTime;
            } else {
                allTime += n + 1;
            }

            // 每次减少任务数量
            for (int i = 0; i < myList.size(); ++i) {
                long count = myList.get(i);
                if (0 < count) {
                    myList.set(i, count - 1);
                }
            }

            // 清除已处理完的任务
            myList = myList.stream().filter(x -> x != 0).collect(Collectors.toList());
        }

        // 输出结果
        System.out.println(allTime);
    }
}